package com.action;

import com.constant.DataConstant;
import com.pojo.AddressVo;
import com.pojo.AreaVo;
import com.pojo.Demo;
import com.pojo.DemoVo;
import com.sun.xml.internal.ws.wsdl.writer.document.http.Address;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Controller
@RequestMapping("demo")
public class DemoController {

    private static List<AddressVo> cacheList = new ArrayList<>();

    /**
     *  省列表
     * */
    @GetMapping("j/province")
    public String province(Model model) {
        model.addAttribute("list", DataConstant.provinces);
        return "jsp/province";
    }

    /**
     *  市列表
     * */
    @GetMapping("t/city")
    public String city(Model model,
                       @RequestParam(value="pid",required = false) Integer pid) {
        model.addAttribute("list",
                null == pid ? new ArrayList<>() :
                DataConstant.cities.stream()
                    .filter(item -> item.getPid() == pid)
                    .collect(Collectors.toList()));
        return "thymeleaf/city";
    }

    /**
     *  区列表
     * */
    @GetMapping("f/area")
    public String area(Model model,
                       @RequestParam(value="pid",required = false) Integer pid) {
        model.addAttribute("list",
                null == pid ? new ArrayList<>() :
                DataConstant.areas.stream()
                    .filter(item -> item.getPid() == pid)
                    .collect(Collectors.toList()));
        return "freemarker/area";
    }

    /**
     *  列表
     * */
    @GetMapping("t/list")
    public String tList(Model model,
                       @RequestParam(value="id") Integer id,
                        @RequestParam(value="type") Integer type) {
        cacheList = getList(type, id);
        model.addAttribute("list", cacheList);
        return "thymeleaf/list";
    }

    /**
     *  详情
     * */
    @GetMapping("f/detail")
    public String fDetail(Model model, @RequestParam(value="id", required = false) Integer id) {
        model.addAttribute("vo", id ==  null
                ? null:
                cacheList.stream()
                .filter(item ->item.getId().intValue() == id.intValue())
                .collect(Collectors.toList()).get(0));
        return "freemarker/detail";
    }

    /**
     *  弹窗
     * */
    @GetMapping("j/dialog")
    public String fDialog(Model model,
                          @RequestParam(value="id") String id) {
        model.addAttribute("id", id);

        return "jsp/dialog";
    }

    /**
     *  添加数据
     * */
    @PostMapping("t/saveList")
    @ResponseBody
    public Map<String, Object> tSaveList(Model model,
                          @RequestBody AddressVo vo) {
        cacheList.add(vo);
        return new HashMap<String, Object>(){{
            put("code", "0");
        }};
    }

    @GetMapping("t/flushList")
    public String tFlushList(Model model) {
        model.addAttribute("list", cacheList);
        return "thymeleaf/list";
    }


    // 生成数据
    private List<AddressVo> getList(Integer type, Integer id){
        List<AreaVo> list = DataConstant.dataMap.get(type);
        AreaVo area = list.stream()
                .filter(item -> item.getId() == id)
                .collect(Collectors.toList()).get(0);
        if(type == 1){
            return IntStream.range(1, 10).mapToObj(i -> new AddressVo(i,area.getName(),
                    area.getName()+"xxxxxx")).collect(Collectors.toList());
        }
        if(type == 2){
            List<AreaVo> l = DataConstant.dataMap.get(1);
            AreaVo a = l.stream()
                    .filter(item -> item.getId() == area.getPid())
                    .collect(Collectors.toList()).get(0);
            return IntStream.range(1, 10).mapToObj(i -> new AddressVo(i,area.getName(),
                    a.getName()+area.getName()+"xxxxxx")).collect(Collectors.toList());
        }
        if(type == 3){
            List<AreaVo> l = DataConstant.dataMap.get(2);
            AreaVo a = l.stream()
                    .filter(item -> item.getId() == area.getPid())
                    .collect(Collectors.toList()).get(0);
            List<AreaVo> l2 = DataConstant.dataMap.get(1);
            AreaVo a2 = l2.stream()
                    .filter(item -> item.getId() == a.getPid())
                    .collect(Collectors.toList()).get(0);
            return IntStream.range(1, 10).mapToObj(i -> new AddressVo(i,area.getName(),
                    a2.getName()+a.getName()+area.getName()+"xxxxxx")).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }




}
